// packageA/pages/scanForPay/CompanyList/index.js
const common = require("../../../public.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cityCouponList: [],
        flag: true //控制开关
    },
    // 店铺优惠券线上券
    copuStoreList() {
        var that = this
        var tempJson = {
            storeId: that.data.storeId,
            couponType: 2, //1线上券，2线下券
            token: that.data.token,
            userId: that.data.userInfo.id
        };
        common.wxRequest("/discountCoupon/getMyCouponForStoreByType", "post", tempJson, 'application/x-www-form-urlencoded', function (res) {
            console.log(res);
            if (res.status == 200) {
                for (var i = 0; i < res.data.length; i++) {
                    res.data[i].beginTime = common.dateFormat('yyyy-MM-dd', res.data[i].beginTime)
                    res.data[i].finishTime = common.dateFormat('yyyy-MM-dd', res.data[i].finishTime)
                    res.data[i].isSelect = false
                }
                var res = res.data
                res.forEach((item, i) => {
                    item.isSelect = false
                })
                that.setData({
                    cityCouponList: res,
                    paymentValue: that.data.paymentValue || 0
                })

            }
        });
    },
    //   线下确定

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        console.log(options, "果然")
        common.getLocalUserInfo(function (success, data) {
            if (success) {
                that.setData({
                    userInfo: data.user,
                    token: data.token,
                    openid: data.openid,
                    storeId: options.storeId,
                    paymentValue: options.paymentValue
                }, function () {
                    that.copuStoreList()
                })
            }
        }, true)
    },
    // 确定按钮  userDiscountCouponId udcId
    clcik_submit() {
        let obj = this.data.cityCouponList.find(x => x.isSelect == true)
        console.log(obj,"obj")
        let userDiscountCouponId = obj ? obj.udcId : ''
        console.log( obj,"objjjjjjjjjjj" )
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2]; // 返会上个界面
        prevPage.setData({ // 返回携带参数给上个界面
            userDiscountCouponId,
            discountsMoney:obj ? obj.discountsMoney  :''
        });
        wx.navigateBack({ // 返会上个界面
            delta: 1
        })
    },
    checkedS(e) {
        var that = this
        let {
            index,
            item
        } = e.currentTarget.dataset
        if (that.data.paymentValue < item.exceedMoney) {
            common.showToast('付款金额低于满减金额')
            return
        }
        for (var i = 0; i < this.data.cityCouponList.length; i++) {
            if (index == i) {
                this.data.cityCouponList[i].isSelect = !this.data.cityCouponList[i].isSelect
            } else {
                this.data.cityCouponList[i].isSelect = false
            }
        }
        this.setData({
            cityCouponList: this.data.cityCouponList,
            // flag
        }, () => {

            if (this.data.cityCouponList.findIndex(targrt => targrt.isSelect == true) == -1) {
                that.setData({
                    flag: true
                })
            } else {
                that.setData({
                    flag: false
                })
            }
        })

    },
   
})